package com.yunfeisoft.base.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.model.WorkflowInstance;
import com.yunfeisoft.base.model.WorkflowTask;
import com.yunfeisoft.base.service.inter.WorkflowTaskService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: WorkflowTaskController
 * Description: 流程实例执行Controller
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@RestController
public class WorkflowTaskController extends BaseController {

    @Autowired
    private WorkflowTaskService workflowTaskService;

    /**
     * 查询流程实例执行
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowTask/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        WorkflowTask record = workflowTaskService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询流程实例执行
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowTask/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String title = ServletRequestUtils.getStringParameter(request, "title", null);
        String instanceCreateName = ServletRequestUtils.getStringParameter(request, "instanceCreateName", null);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);

        User loginUser = ApiUtils.getLoginUser();

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("title", title);
        params.put("instanceCreateName", instanceCreateName);
        params.put("executor", loginUser.getId());
        if (status > 0) {
            params.put("status", status);
        }

        Page<WorkflowTask> page = workflowTaskService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除流程实例执行
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowTask/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        workflowTaskService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 同意审批任务
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowTask/approvalTask", method = {RequestMethod.POST, RequestMethod.GET})
    public Response approvalTask(HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        //validator.required(request, "taskId", "任务参数错误");
        validator.required(request, "assigneeId", "处理人为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String taskId = ServletRequestUtils.getStringParameter(request, "taskId", null);
        String instanceId = ServletRequestUtils.getStringParameter(request, "instanceId", null);
        String assigneeId = ServletRequestUtils.getStringParameter(request, "assigneeId", null);
        String remark = ServletRequestUtils.getStringParameter(request, "remark", null);

        if (StringUtils.isBlank(taskId) && StringUtils.isBlank(instanceId)) {
            return ResponseUtils.warn("任务或者流程实例参数错误");
        }

        if ("-100".equals(assigneeId)) {
            assigneeId = null;
        }

        int isSubmit = ServletRequestUtils.getIntParameter(request, "isSubmit", -1);
        if (isSubmit == 1) {
            //remark = "发起审批";

            workflowTaskService.modifyAndSubmitProcess(instanceId, assigneeId);
            return ResponseUtils.success("提交成功");
        }

        if (StringUtils.isBlank(remark)) {
            remark = "同意";
        }

        workflowTaskService.modifyAndExecuteTask(taskId, WorkflowInstance.WorkflowInstanceStatusEnum.ACCEPT.getValue(), remark, assigneeId);
        return ResponseUtils.success("审批成功");
    }

    /**
     * 驳回审批任务
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowTask/rejectTask", method = {RequestMethod.POST, RequestMethod.GET})
    public Response rejectTask(HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "taskId", "任务参数错误");
        validator.required(request, "remark", "驳回原因为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String taskId = ServletRequestUtils.getStringParameter(request, "taskId", null);
        String remark = ServletRequestUtils.getStringParameter(request, "remark", null);

        workflowTaskService.modifyAndExecuteTask(taskId, WorkflowInstance.WorkflowInstanceStatusEnum.REJECT.getValue(), remark, null);
        return ResponseUtils.success("驳回成功");
    }
}
